﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SMALLERP.ComClass;
using SMALLERP.DataClass;
using System.Data.SqlClient;

namespace SMALLERP.SO
{
    public partial class SOQuery : Form
    {
        private readonly DataBase db = new DataBase();
        private SqlDataReader sdr1;
        public SOQuery()
        {
            InitializeComponent();
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            string strSql = null;

            strSql = "SELECT TD001,TD002,TD003,MA002,TC012,TD004,TD005,TD006,TD008,TD009,TV035,MB029,TC003,TD013 ";
            strSql += "FROM COPTD LEFT JOIN COPTC ON TD001=TC001 AND TD002=TC002 LEFT JOIN COPMA ON TC004=MA001 LEFT JOIN INVMB ON TD004=MB001 LEFT JOIN MOCTA ON TD001=TA026 AND TD002=TA027 AND TD003=TA028 LEFT JOIN QMSTV ON TA001=TV001 AND TA002=TV002";
            strSql += " WHERE TD016='N' AND TA006 LIKE '2%' AND MA002 LIKE '%" + textBox1.Text.ToString() + "%' AND TC012 LIKE '%" + textBox2.Text.ToString() + "%' AND TD006 LIKE '%" + textBox5.Text.ToString() + "%'  AND MB029 LIKE '%" + textBox6.Text.ToString() + "%'";

            try
            {
                dataGridView1.DataSource = db.GetDataSet1(strSql, "COPTD").Tables["COPTD"];
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "软件提示");
                throw ex;
            }
        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {
            string strSql = null;

            strSql = "SELECT TD001,TD002,TD003,MA002,TC012,TD004,TD005,TD006,TD008,TD009,TV035,MB029,TC003 ";
            strSql += "FROM COPTD LEFT JOIN COPTC ON TD001=TC001 AND TD002=TC002 LEFT JOIN COPMA ON TC004=MA001 LEFT JOIN INVMB ON TD004=MB001 LEFT JOIN MOCTA ON TD001=TA026 AND TD002=TA027 AND TD003=TA028 LEFT JOIN QMSTV ON TA001=TV001 AND TA002=TV002";
            strSql += " WHERE TD016='N' AND TA006 LIKE '2%' AND MA002 LIKE '%" + textBox1.Text.ToString() + "%' AND TC012 LIKE '%" + textBox2.Text.ToString() + "%' AND TD006 LIKE '%" + textBox5.Text.ToString() + "%'  AND MB029 LIKE '%" + textBox6.Text.ToString() + "%'";

            try
            {
                dataGridView1.DataSource = db.GetDataSet1(strSql, "COPTD").Tables["COPTD"];
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "软件提示");
                throw ex;
            }
        }

        private void textBox5_TextChanged(object sender, EventArgs e)
        {
            string strSql = null;

            strSql = "SELECT TD001,TD002,TD003,MA002,TC012,TD004,TD005,TD006,TD008,TD009,TV035,MB029,TC003 ";
            strSql += "FROM COPTD LEFT JOIN COPTC ON TD001=TC001 AND TD002=TC002 LEFT JOIN COPMA ON TC004=MA001 LEFT JOIN INVMB ON TD004=MB001 LEFT JOIN MOCTA ON TD001=TA026 AND TD002=TA027 AND TD003=TA028 LEFT JOIN QMSTV ON TA001=TV001 AND TA002=TV002";
            strSql += " WHERE TD016='N' AND TA006 LIKE '2%' AND MA002 LIKE '%" + textBox1.Text.ToString() + "%' AND TC012 LIKE '%" + textBox2.Text.ToString() + "%' AND TD006 LIKE '%" + textBox5.Text.ToString() + "%'  AND MB029 LIKE '%" + textBox6.Text.ToString() + "%'";

            try
            {
                dataGridView1.DataSource = db.GetDataSet1(strSql, "COPTD").Tables["COPTD"];
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "软件提示");
                throw ex;
            }
        }

        private void textBox6_TextChanged(object sender, EventArgs e)
        {
            string strSql = null;

            strSql = "SELECT TD001,TD002,TD003,MA002,TC012,TD004,TD005,TD006,TD008,TD009,TV035,MB029,TC003 ";
            strSql += "FROM COPTD LEFT JOIN COPTC ON TD001=TC001 AND TD002=TC002 LEFT JOIN COPMA ON TC004=MA001 LEFT JOIN INVMB ON TD004=MB001 LEFT JOIN MOCTA ON TD001=TA026 AND TD002=TA027 AND TD003=TA028 LEFT JOIN QMSTV ON TA001=TV001 AND TA002=TV002";
            strSql += " WHERE TD016='N' AND TA006 LIKE '2%' AND MA002 LIKE '%" + textBox1.Text.ToString() + "%' AND TC012 LIKE '%" + textBox2.Text.ToString() + "%' AND TD006 LIKE '%" + textBox5.Text.ToString() + "%'  AND MB029 LIKE '%" + textBox6.Text.ToString() + "%'";

            try
            {
                dataGridView1.DataSource = db.GetDataSet1(strSql, "COPTD").Tables["COPTD"];
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "软件提示");
                throw ex;
            }
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {

        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
           
        }

        private void dataGridView1_SelectionChanged(object sender, EventArgs e)
        {
            string strSql = null;
            strSql = "SELECT XA002,XA009,XA011,XA023,XA012,XA042 ";
            strSql += "FROM COPXA";
            strSql += " WHERE XA016='" + dataGridView1["TD001", dataGridView1.CurrentCell.RowIndex].Value.ToString() + "' AND XA017='" + dataGridView1["TD002", dataGridView1.CurrentCell.RowIndex].Value.ToString() + "' AND XA018='" + dataGridView1["TD003", dataGridView1.CurrentCell.RowIndex].Value.ToString() + "'";

            try
            {
                
                        dataGridView2.DataSource = db.GetDataSet1(strSql, "COPXA").Tables["COPXA"];
                  
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "软件提示");
                throw ex;
            }
            strSql = "SELECT TG003,TH008 ";
            strSql += "FROM COPTH LEFT JOIN COPTG ON TH001=TG001 AND TH002=TG002";
            strSql += " WHERE TH014='" + dataGridView1["TD001", dataGridView1.CurrentCell.RowIndex].Value.ToString() + "' AND TH015='" + dataGridView1["TD002", dataGridView1.CurrentCell.RowIndex].Value.ToString() + "' AND TH016='" + dataGridView1["TD003", dataGridView1.CurrentCell.RowIndex].Value.ToString() + "' ORDER BY TG003";

            try
            {
                        dataGridView3.DataSource = db.GetDataSet1(strSql, "COPTH").Tables["COPTH"];
                   
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "软件提示");
                throw ex;
            }
        }

        private void SOQuery_Load(object sender, EventArgs e)
        {
            string strSql = null;

            strSql = "SELECT TD001,TD002,TD003,MA002,TC012,TD004,TD005,TD006,TD008,TD009,TV035,MB029,TC003,TD013 ";
            strSql += "FROM COPTD LEFT JOIN COPTC ON TD001=TC001 AND TD002=TC002 LEFT JOIN COPMA ON TC004=MA001 LEFT JOIN INVMB ON TD004=MB001 LEFT JOIN MOCTA ON TD001=TA026 AND TD002=TA027 AND TD003=TA028 LEFT JOIN QMSTV ON TA001=TV001 AND TA002=TV002";
            strSql += " WHERE TD016='N' AND TA006 LIKE '2%' AND MA002 LIKE '%" + textBox1.Text.ToString() + "%' AND TC012 LIKE '%" + textBox2.Text.ToString() + "%' AND TD006 LIKE '%" + textBox5.Text.ToString() + "%'  AND MB029 LIKE '%" + textBox6.Text.ToString() + "%'";

            try
            {
                dataGridView1.DataSource = db.GetDataSet1(strSql, "COPTD").Tables["COPTD"];
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "软件提示");
                throw ex;
            }
        }
    }
}
